//
//  MessageViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/13.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "MessageViewController.h"

#import "JifenDetailViewController.h"
#import "YueDetailViewController.h"
#import "WebNumberViewController.h"
#import "GerenDetailViewController.h"
#import "SetViewController.h"
#import "QQOnlineViewController.h"

#import "LoginViewController.h"

@interface MessageViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

//个人头像列
@property (nonatomic, strong) UIImageView * image_touxiang;
@property (nonatomic, strong) UILabel * name;
@property (nonatomic, strong) UILabel * level;

@property (nonatomic, strong) UILabel * text_1;
@property (nonatomic, strong) UILabel * text_2;
@property (nonatomic, strong) UILabel * text_3;
@property (nonatomic, strong) UILabel * text_4;
@property (nonatomic, strong) UILabel * text_5;

@property (nonatomic, strong) UIButton * btn_cancel;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"个人中心"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
//    _btnRight.hidden = YES;
    _lblRight.text = @"设置";
    
    _btnLeft.hidden = YES;
}

//显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [self p_data_userInfo];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
    
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

//设置
- (void)clickRightButton:(UIButton *)sender
{
    SetViewController * setViewController = [[SetViewController alloc] init];
    [self showViewController:setViewController sender:nil];
    
//    LoginViewController * loginViewController = [[LoginViewController alloc] init];
//    [self showViewController:loginViewController sender:nil];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + 5 + 1;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return 130;
    }
    else if(indexPath.row == 6)
    {
        return 110;
    }
    else
    {
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    switch (indexPath.row)
    {
        case 0:
        {
            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UIImageView * view_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
            view_bg.image = [UIImage imageNamed:@"whith_bg.png"];
            [cell addSubview:view_bg];
            
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {//登录状态下
                self.image_touxiang = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 70, 70)];
                self.image_touxiang.layer.cornerRadius = 35;
                self.image_touxiang.layer.masksToBounds = YES;
//                self.image_touxiang.backgroundColor = [UIColor orangeColor];
                [cell addSubview:self.image_touxiang];
                
                
                self.name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.image_touxiang.frame) + 10, 30, SCREEN_WIDTH - CGRectGetMaxX(self.image_touxiang.frame) - 20, 25)];
//                self.name.text = @"智顺网络科技公司";
                [cell addSubview:self.name];
                
                
                self.level = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.image_touxiang.frame) + 10, CGRectGetMaxY(self.name.frame) + 5, SCREEN_WIDTH - CGRectGetMaxX(self.image_touxiang.frame) - 20, 20)];
                self.level.text = [userdefault objectForKey:@"title"];
                self.level.textColor = [UIColor orangeColor];
                self.level.font = [UIFont systemFontOfSize:13];
                [cell addSubview:self.level];
                
                
                [self.image_touxiang sd_setImageWithURL:[userdefault objectForKey:@"head_pic"] placeholderImage:[UIImage imageNamed:@"01-120.png"]];
                self.name.text = [userdefault objectForKey:@"nickname"];
            }
            else
            {//未登录状态下
                UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 35, 20, 70, 70)];
                image.layer.cornerRadius = 35;
                image.layer.masksToBounds = YES;
                image.image = [UIImage imageNamed:@"01-120.png"];
//                image.backgroundColor = [UIColor orangeColor];
                [cell addSubview:image];
                
                
                UIButton * btn_login = [UIButton buttonWithType:(UIButtonTypeSystem)];
                btn_login.frame = CGRectMake(SCREEN_WIDTH / 2 - 35, 20, 70, 70);
                btn_login.layer.cornerRadius = 35;
                [btn_login addTarget:self action:@selector(btn_loginAction:) forControlEvents:(UIControlEventTouchUpInside)];
                [cell addSubview:btn_login];
            }
        }
            break;
        case 1:
        {
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 25, 25)];
            image.image = [UIImage imageNamed:@"chanpin@2x.png"];
            [cell addSubview:image];
            
            self.text_1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 50, 20)];
            self.text_1.text = @"站点";
            [cell addSubview:self.text_1];
            
            UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20)];
            image_return.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image_return];
            
            UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(30, 49, SCREEN_WIDTH - 30, 1)];
            view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [cell addSubview:view_line];
            
            //
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                self.text_1.text = [NSString stringWithFormat:@"站点 %@个",[userdefault objectForKey:@"servicenum"]];
            }
        }
            break;
        case 2:
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 25, 25)];
            image.image = [UIImage imageNamed:@"yue@2x.png"];
            [cell addSubview:image];
            
            self.text_2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 50, 20)];
            self.text_2.text = @"余额";
            [cell addSubview:self.text_2];
            
            UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20)];
            image_return.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image_return];
            
            UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(30, 49, SCREEN_WIDTH - 30, 1)];
            view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [cell addSubview:view_line];
            
            //
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                self.text_2.text = [NSString stringWithFormat:@"余额 %@",[userdefault objectForKey:@"balance"]];

                //余额为负
                NSString * str_first = [[userdefault objectForKey:@"balance"] substringToIndex:1];
                if([str_first isEqualToString:@"-"])
                {
                    self.text_2.textColor = [UIColor redColor];
                }
            }
        }
            break;
        case 3:
        {
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 25, 25)];
            image.image = [UIImage imageNamed:@"jifen@2x.png"];
            [cell addSubview:image];
            
            self.text_3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 50, 20)];
            self.text_3.text = @"积分";
            [cell addSubview:self.text_3];
            
            UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20)];
            image_return.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image_return];
            
            UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(30, 49, SCREEN_WIDTH - 30, 1)];
            view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [cell addSubview:view_line];
            
            //
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                self.text_3.text = [NSString stringWithFormat:@"积分 %@",[userdefault objectForKey:@"score"]];
            }
        }
            break;
        case 4:
        {
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 25, 25)];
            image.image = [UIImage imageNamed:@"kefu@2x.png"];
            [cell addSubview:image];
            
            self.text_4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 50, 20)];
            self.text_4.text = @"客服 400-0539-588";
            [cell addSubview:self.text_4];
            
            UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20)];
            image_return.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image_return];
            
            UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(30, 49, SCREEN_WIDTH - 30, 1)];
            view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [cell addSubview:view_line];
            
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                self.text_4.text = [NSString stringWithFormat:@"客服 %@",[userdefault objectForKey:@"customer_service"]];
            }
        }
            break;
        case 5:
        {
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 25, 25)];
            image.image = [UIImage imageNamed:@"QQkefu@2x.png"];
            [cell addSubview:image];
            
            self.text_4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image.frame) - 50, 20)];
            self.text_4.text = @"QQ在线客服";
            [cell addSubview:self.text_4];
            
            UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 15, 20, 20)];
            image_return.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image_return];
            
            UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(30, 49, SCREEN_WIDTH - 30, 1)];
            view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [cell addSubview:view_line];
        }
            break;
        case 6:
        {
            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            UIImageView * image_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 50)];
            image_bg.image = [UIImage imageNamed:@"whith_bg.png"];
            [cell addSubview:image_bg];
            
            self.btn_cancel = [UIButton buttonWithType:(UIButtonTypeSystem)];
            self.btn_cancel.frame = CGRectMake(0, 25, SCREEN_WIDTH, 50);
            self.btn_cancel.titleLabel.font = [UIFont systemFontOfSize:18];
            [cell addSubview:self.btn_cancel];
            
            [self.btn_cancel addTarget:self action:@selector(btn_cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            //
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                [self.btn_cancel setTitle:@"退出登录" forState:(UIControlStateNormal)];
                [self.btn_cancel setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
            }
            else
            {
                [self.btn_cancel setTitle:@"立即登录" forState:(UIControlStateNormal)];
                [self.btn_cancel setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            }
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];

    switch (indexPath.row) {
        case 0:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                GerenDetailViewController * gerenDetailViewController = [[GerenDetailViewController alloc] init];
                [self showViewController:gerenDetailViewController sender:nil];
            }
//            else
//            {
//                LoginViewController * loginViewController = [[LoginViewController alloc] init];
//                [self showViewController:loginViewController sender:nil];
//            }
        }
            break;
        case 1:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                WebNumberViewController * webNumberViewController = [[WebNumberViewController alloc] init];
                
                [self showViewController:webNumberViewController sender:nil];
            }
            else
            {
                LoginViewController * loginViewController = [[LoginViewController alloc] init];
                [self showViewController:loginViewController sender:nil];
            }
        }
            break;
        case 2:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"0"])
            {
                LoginViewController * loginViewController = [[LoginViewController alloc] init];
                [self showViewController:loginViewController sender:nil];
            }
        }
            break;
        case 3:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                JifenDetailViewController * jifenDetailViewController = [[JifenDetailViewController alloc] init];
                
                [self showViewController:jifenDetailViewController sender:nil];
            }
            else
            {
                LoginViewController * loginViewController = [[LoginViewController alloc] init];
                [self showViewController:loginViewController sender:nil];
            }
        }
            break;
        case 4:
        {
            if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
            {
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"客服电话:%@",[userdefault objectForKey:@"customer_service"]] preferredStyle:(UIAlertControllerStyleAlert)];
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
                
                UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:action];
                
                UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"拨打" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                    NSURL * url_tel = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[userdefault objectForKey:@"customer_service"]]];
                    
                    [[UIApplication sharedApplication] openURL:url_tel];
                }];
                [alert addAction:action1];
            }
            else
            {
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"客服电话:400-0539-588" preferredStyle:(UIAlertControllerStyleAlert)];
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
                
                UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:action];
                
                
                UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"拨打" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                    NSURL * url_tel = [NSURL URLWithString:[NSString stringWithFormat:@"tel://400-0539-588"]];
                    
                    [[UIApplication sharedApplication] openURL:url_tel];
                    
                }];
                [alert addAction:action1];
            }
        }
            break;
        case 5:
        {
            QQOnlineViewController * qqOnlineViewController = [[QQOnlineViewController alloc] init];
            
            [self showViewController:qqOnlineViewController sender:nil];
        }
        default:
            break;
    }
}

#pragma mark - 退出登录
- (void)btn_cancelAction:(UIButton *)sender
{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];

    if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认退出登录" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        
        
        UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            //保存退出的状态
            [userdefault setObject:@"0" forKey:@"Login_Success"];
            
            
            [SVProgressHUD setMinimumDismissTimeInterval:1.0f];
            [SVProgressHUD showSuccessWithStatus:@"成功退出"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                [self.tableView reloadData];
            });
        }];
        
        [alert addAction:action1];
    }
    else
    {
        LoginViewController * loginViewController = [[LoginViewController alloc] init];
        [self showViewController:loginViewController sender:nil];
    }
}

#pragma mark - 登录按钮
- (void )btn_loginAction:(UIButton *)sender
{
    LoginViewController * loginViewController = [[LoginViewController alloc] init];
    [self showViewController:loginViewController sender:nil];
}

#pragma mark - 用户基本信息请求
- (void)p_data_userInfo
{//如果处于登录状态,每进这个页面就更新下数据
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
    {
        DataProvider * dataprovider = [[DataProvider alloc] init];
        
        [dataprovider setDelegateObject:self setBackFunctionName:@"userInfo:"];
        
        [dataprovider userInfoWithUid:[userdefault objectForKey:@"uid"] token:[userdefault objectForKey:@"token"]];
    }
}

#pragma mark - 接口部分
- (void)userInfo:(id )dict
{
//    NSLog(@"%@",dict);
    if([dict[@"status"] integerValue] == 1)
    {
        @try
        {
            NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
            
            //保存用户信息（后期可能更多）
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"uid"]] forKey:@"uid"];
            //头像
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"avatar64"]] forKey:@"head_pic"];
            //站点数
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"servicenum"]] forKey:@"servicenum"];
            //余额
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"balance"]] forKey:@"balance"];
            //昵称
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"nickname"]] forKey:@"nickname"];
            //积分
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"score"]] forKey:@"score"];
            
            //客服电话
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"customer_service"]] forKey:@"customer_service"];
            //头衔vip
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"title"]] forKey:@"title"];
            
            //关键词个数
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"keywordnum"]] forKey:@"keywordnum"];
            
        }
        @catch (NSException *exception)
        {
            
        }
        @finally
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                [self.tableView reloadData];
            });
        }
    }
}



@end
